<template>
  <el-card class="form-container" shadow="never">
    <el-form
      :model="policy"
      :rules="rules"
      ref="policyFrom"
      label-width="120px"
       style="width: 720px" 
      size="small"
    >
      <el-form-item label="标题：" prop="title">
        <el-input v-model="policy.title" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="来源：" prop="source">
        <el-input v-model="policy.source" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="上线/下线：" prop="status">
        <el-radio-group v-model="policy.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">上线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="浏览量：" v-if="isEdit">
        <el-input
          v-model="policy.viewCount"
          class="input-width"
          disabled
        ></el-input>
      </el-form-item>
      <el-form-item label="排序：" prop="sort">
        <el-input v-model="policy.sort" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="内容：" prop="content">
        <tinymce :width="595" :height="300" v-model="policy.content"></tinymce>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('policyFrom')"
          >提交</el-button
        >
        <el-button v-if="!isEdit" @click="resetForm('policyFrom')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import SingleUpload from "@/components/Upload/singleUpload";
import { createPolicy, getPolicy, updatePolicy } from "@/api/policy";
import Tinymce from "@/components/Tinymce";
const defaultPolicy = {
  source: null,
  viewCount: null,
  content: null,
  title: null,
  status: 1,
  sort: 0,
};
export default {
  name: "policyDetail",
  components: { SingleUpload, Tinymce },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      policy: Object.assign({}, defaultPolicy),
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "长度在 2 到 100 个字符",
            trigger: "blur",
          },
        ],
        source: { required: true, message: "请输入来源", trigger: "blur" },
        content: { required: true, message: "请输入内容", trigger: "blur" },
        sort: { required: true, message: "请输入来源", trigger: "blur" },
      },
    };
  },
  created() {
    if (this.isEdit) {
      getPolicy(this.$route.query.id).then((response) => {
        this.policy = response.data;
      });
    } else {
      this.policy = Object.assign({}, defaultPolicy);
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            if (this.isEdit) {
              updatePolicy(this.$route.query.id, this.policy).then(
                (response) => {
                  this.$message({
                    message: "修改成功",
                    type: "success",
                    duration: 1000,
                  });
                  this.$router.back();
                }
              );
            } else {
              createPolicy(this.policy).then((response) => {
                this.policy = Object.assign({}, defaultPolicy);
                this.$message({
                  message: "提交成功",
                  type: "success",
                  duration: 1000,
                });
              });
            }
          });
        } else {
          this.$message({
            message: "请完整填写内容后提交",
            type: "error",
            duration: 1000,
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.policy = Object.assign({}, defaultPolicy);
    },
  },
};
</script>
<style scoped>
.input-width {
  width: 70%;
}
.form-container {
    width: 800px;
  }
</style>


